﻿Public Class main

    'Variable declarations
    Private saved As Boolean
    Private flag_gen As Boolean
    Private msgrslt1, msgrslt2, msgrslt3 As Microsoft.VisualBasic.MsgBoxResult
    Private l As New language
    Dim language As String
    Dim currentfile As String
    Property ProjectPath As String

    'Property declarations
    Property prjname() As String
        Get
            Return solnBrowse.getroot()
        End Get
        Set(ByVal value As String)
            solnBrowse.setroot(value)
        End Set
    End Property

    Private Sub enableall(ByVal cnd As Boolean)
        TableLayoutPanel1.Enabled = cnd
        SaveToolStripMenuItem.Enabled = cnd
        CloseToolStripMenuItem1.Enabled = cnd
        CleanToolStripMenuItem.Enabled = cnd
        BuildToolStripMenuItem.Enabled = cnd
        ToolStripMenuItem2.Enabled = cnd
        ToolStripMenuItem3.Enabled = cnd
        BuildActionToolStripMenuItem.Enabled = cnd
        VersionToolStripMenuItem.Enabled = cnd
        SetToolStripMenuItem.Enabled = cnd
        RebaseToolStripMenuItem.Enabled = cnd
        GitCommitToolStripMenuItem.Enabled = cnd
        UploadToolStripMenuItem.Enabled = cnd
        TableLayoutPanel1.Visible = cnd
    End Sub

    Private Sub save(ByVal t As System.Windows.Forms.TabPage)
        Dim a As New System.IO.StreamWriter(t.Text)
        a.Flush()
        a.Write(t.Controls(t.Name).Text)
        a.Close()
    End Sub

    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        about.Show()
    End Sub

    Private Sub CloseToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem1.Click
        msgrslt3 = MsgBox("Do you wnat to close whole project?", MsgBoxStyle.YesNo, "OmniSource")
        If msgrslt3 = MsgBoxResult.Yes Then
            If saved = True Then
                solnBrowse.RootPath = "C:\"
                solnBrowse.Visible = False
                solnBrowse.rel()
                TableLayoutPanel1.Enabled = False
                TableLayoutPanel1.Visible = False
            Else
                msgrslt1 = MsgBox("Do you want to save changes?", vbOKCancel, "Save changes")
                If (msgrslt1 = vbOK) Then
                    For Each t As System.Windows.Forms.TabPage In editorPanel.TabPages
                        save(t)
                    Next
                Else
                    solnBrowse.RootPath = "C:\"
                    solnBrowse.Visible = False
                    solnBrowse.rel()
                End If
                TableLayoutPanel1.Enabled = False
                TableLayoutPanel1.Visible = False
                enableall(False)
            End If
        Else
            closeone(editorPanel.SelectedTab)
        End If
    End Sub

    Private Sub LoadToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadToolStripMenuItem.Click
        If (projectLoc.ShowDialog() = DialogResult.OK) Then
            enableall(True)
            solnBrowse.RootPath = projectLoc.SelectedPath
            ProjectPath = projectLoc.SelectedPath
            solnBrowse.Visible = True
            If flag_gen = True Then
                solnBrowse.rel()
            End If
            flag_gen = True
            language = l.get_lan(ProjectPath)
            config()
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        saved = False
        flag_gen = False
        TableLayoutPanel1.Enabled = False
        TableLayoutPanel1.Visible = False
    End Sub

    Private Sub ToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem2.Click
        configure.Show()
    End Sub

    Private Sub BuildToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BuildToolStripMenuItem.Click
        FileIO.FileSystem.CurrentDirectory = Me.ProjectPath
        If FileIO.FileSystem.FileExists(Me.ProjectPath & "\build.bat") Then
            Dim p As New System.Diagnostics.ProcessStartInfo()
            p.FileName = "build.bat"
            p.WorkingDirectory = Me.ProjectPath
            Process.Start(p)
        Else
            msgrslt2 = MsgBox("Build action not specified yet! Do you want to specify!?", MsgBoxStyle.YesNo, "Build action not specified")
            If msgrslt2 = MsgBoxResult.Yes Then
                build.Show()
            End If
        End If
    End Sub

    Private Sub CleanToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CleanToolStripMenuItem.Click
        Dim c As New Cleaner(ProjectPath)
        c.cleantree()
    End Sub

    Private Sub UploadToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UploadToolStripMenuItem.Click
        MsgBox("Implemented in v1.x")
    End Sub

    Private Sub SetToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetToolStripMenuItem.Click
        version.Show()
    End Sub

    Private Sub GitCommitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GitCommitToolStripMenuItem.Click
        MsgBox("Implemented in v1.x")
    End Sub

    Private Sub UpdateToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateToolStripMenuItem.Click
        Dim target As String = "http://code.google.com/p/omnisource/"
        System.Diagnostics.Process.Start(target)
    End Sub

    Private Sub BuildActionToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BuildActionToolStripMenuItem.Click
        build.Show()
    End Sub

    Private Sub solnBrowse_file_clicked(ByVal file As String, ByVal name As String) Handles solnBrowse.file_clicked
        For Each t As System.Windows.Forms.TabPage In editorPanel.TabPages()
            If t.Name = name Then
                editorPanel.SelectTab(name)
                editorPanel.Focus()
                Exit Sub
            End If
        Next
        If FileIO.FileSystem.FileExists(file) Then
            Dim reader As New System.IO.StreamReader(file)
            currentfile = file
            Dim tp As New System.Windows.Forms.TabPage(name)
            tp.Name = name
            tp.Text = file
            Dim editor As New System.Windows.Forms.TextBox
            editor.Dock = DockStyle.Fill
            editor.Font = New System.Drawing.Font("Consolas", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
            editor.Multiline = True
            editor.Name = name
            editorPanel.TabPages.Add(tp)
            tp.Controls.Add(editor)
            Do While reader.Peek() <> -1
                editor.Text = reader.ReadToEnd()
            Loop
            reader.Close()
            editorPanel.SelectTab(name)
            editorPanel.Focus()
        Else
            MsgBox("Unexpected error, Please restart OmniSource")
        End If
    End Sub

    Private Sub editor_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        saved = False
    End Sub
    Private Sub RebaseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RebaseToolStripMenuItem.Click
        Dim ev As New Exploreversion(solnBrowse.getroot())
        ev.ShowDialog()
    End Sub

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
    End Sub

    Private Sub closeone(ByVal a As System.Windows.Forms.TabPage)
        Dim w As New System.IO.StreamWriter(a.Text)
        w.Write(editorPanel.Controls(a.Name).Text)
        w.Close()
        a.Hide()
    End Sub

    Private Sub config()
        configure.ShowDialog()
        version.ShowDialog()
        build.ShowDialog()
    End Sub

End Class
